package com.yanqu.road.dao.impl.kuang.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.enums.kuang.eKuangAttributeType;
import com.yanqu.road.entity.kuang.battle.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossKuangBattleUserDataDaoImpl extends TempDataObjectDao<CrossKuangBattleUserData> {

    public static String getCrossKuangBattleUserDataSql = "select * from `t_c_kuang_battle_user_data` where `weekId`=? and `groupId`=? ";

    public Map<Long, CrossKuangBattleUserData> getCrossKuangBattleUserDataMap(int weekId, int groupId) {
        Map<Long, CrossKuangBattleUserData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossKuangBattleUserDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossKuangBattleUserData parse = parse(rs);
                map.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertCrossKuangBattleUserDataSql = "insert into `t_c_kuang_battle_user_data`(weekId,groupId,userId,serverId,attributeMap,campId,placeId,position,transferTime,attackTime,attackedTime,reviveTime,reviveCnt,updatePropertyTime,battleRecord,totalBattleRecordMap,attackScoreMap,seizeScoreMap,updateScoreTime,lastPingPlaceTime,buffList,skillList,personRankReward,groupRankReward,enterBattle) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossKuangBattleUserData crossKuangBattleUserData) {
        crossKuangBattleUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleUserData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getAttributeMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleUserData.getCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleUserData.getPlaceId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getPosition())));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getTransferTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getAttackTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getAttackedTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getReviveTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleUserData.getReviveCnt()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getUpdatePropertyTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getBattleRecord())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getTotalBattleRecordMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getAttackScoreMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getSeizeScoreMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getUpdateScoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getLastPingPlaceTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getBuffList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getSkillList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossKuangBattleUserData.isPersonRankReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossKuangBattleUserData.isGroupRankReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossKuangBattleUserData.isEnterBattle()));
        if (getDb().executeNoneQuery(insertCrossKuangBattleUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    public static String updateCrossKuangBattleUserDataSql = "update `t_c_kuang_battle_user_data` set serverId=?,attributeMap=?,campId=?,placeId=?,position=?,transferTime=?,attackTime=?,attackedTime=?,reviveTime=?,reviveCnt=?,updatePropertyTime=?,battleRecord=?,totalBattleRecordMap=?,attackScoreMap=?,seizeScoreMap=?,updateScoreTime=?,lastPingPlaceTime=?,buffList=?,skillList=?,personRankReward=?,groupRankReward=?,enterBattle=? where weekId=? and groupId=? and userId=?";

    @Override
    protected boolean updateData(CrossKuangBattleUserData crossKuangBattleUserData) {
        crossKuangBattleUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getAttributeMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleUserData.getCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleUserData.getPlaceId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getPosition())));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getTransferTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getAttackTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getAttackedTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getReviveTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleUserData.getReviveCnt()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getUpdatePropertyTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getBattleRecord())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getTotalBattleRecordMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getAttackScoreMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getSeizeScoreMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getUpdateScoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getLastPingPlaceTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getBuffList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleUserData.getSkillList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossKuangBattleUserData.isPersonRankReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossKuangBattleUserData.isGroupRankReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossKuangBattleUserData.isEnterBattle()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleUserData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossKuangBattleUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossKuangBattleUserData parseData(ResultSet rs) throws SQLException {
        CrossKuangBattleUserData crossKuangBattleUserData = new CrossKuangBattleUserData();
        crossKuangBattleUserData.setWeekId(rs.getInt("weekId"));
        crossKuangBattleUserData.setGroupId(rs.getInt("groupId"));
        crossKuangBattleUserData.setUserId(rs.getLong("userId"));
        crossKuangBattleUserData.setServerId(rs.getLong("serverId"));
        String attributeMap = rs.getString("attributeMap");
        if (!StringUtils.isNullOrEmpty(attributeMap)) {
            crossKuangBattleUserData.setAttributeMap(JSON.parseObject(attributeMap, new TypeReference<Map<eKuangAttributeType, Long>>() {}));
        }
        crossKuangBattleUserData.setCampId(rs.getInt("campId"));
        crossKuangBattleUserData.setPlaceId(rs.getInt("placeId"));
        String position = rs.getString("position");
        if (!StringUtils.isNullOrEmpty(position)) {
            crossKuangBattleUserData.setPosition(JSON.parseObject(position, new TypeReference<CrossKuangBattlePosition>() {
            }));
        }
        crossKuangBattleUserData.setTransferTime(rs.getLong("transferTime"));
        crossKuangBattleUserData.setAttackTime(rs.getLong("attackTime"));
        crossKuangBattleUserData.setAttackedTime(rs.getLong("attackedTime"));
        crossKuangBattleUserData.setReviveTime(rs.getLong("reviveTime"));
        crossKuangBattleUserData.setReviveCnt(rs.getInt("reviveCnt"));
        crossKuangBattleUserData.setUpdatePropertyTime(rs.getLong("updatePropertyTime"));
        String battleRecord = rs.getString("battleRecord");
        if (!StringUtils.isNullOrEmpty(battleRecord)) {
            crossKuangBattleUserData.setBattleRecord(JSON.parseObject(battleRecord, new TypeReference<CrossKuangBattleRecord>() {
            }));
        }
        String totalBattleRecordMap = rs.getString("totalBattleRecordMap");
        if (!StringUtils.isNullOrEmpty(totalBattleRecordMap)) {
            crossKuangBattleUserData.setTotalBattleRecordMap(JSON.parseObject(totalBattleRecordMap, new TypeReference<Map<Integer, CrossKuangBattleRecord>>() {
            }));
        }
        String attackScoreMap = rs.getString("attackScoreMap");
        if (!StringUtils.isNullOrEmpty(attackScoreMap)) {
            crossKuangBattleUserData.setAttackScoreMap(JSON.parseObject(attackScoreMap, new TypeReference<Map<Integer, Long>>() {
            }));
        }
        String seizeScoreMap = rs.getString("seizeScoreMap");
        if (!StringUtils.isNullOrEmpty(attackScoreMap)) {
            crossKuangBattleUserData.setSeizeScoreMap(JSON.parseObject(seizeScoreMap, new TypeReference<Map<Integer, Long>>() {
            }));
        }
        crossKuangBattleUserData.setUpdateScoreTime(rs.getLong("updateScoreTime"));
        crossKuangBattleUserData.setLastPingPlaceTime(rs.getLong("lastPingPlaceTime"));
        String buffList = rs.getString("buffList");
        if (!StringUtils.isNullOrEmpty(buffList)) {
            crossKuangBattleUserData.setBuffList(JSON.parseObject(buffList, new TypeReference<List<CrossKuangBattleUserBuff>>() {
            }));
        }
        String skillList = rs.getString("skillList");
        if (!StringUtils.isNullOrEmpty(skillList)) {
            crossKuangBattleUserData.setSkillList(JSON.parseObject(skillList, new TypeReference<List<CrossKuangBattleUserSkill>>() {
            }));
        }
        crossKuangBattleUserData.setPersonRankReward(rs.getBoolean("personRankReward"));
        crossKuangBattleUserData.setGroupRankReward(rs.getBoolean("groupRankReward"));
        crossKuangBattleUserData.setEnterBattle(rs.getBoolean("enterBattle"));
        return crossKuangBattleUserData;
    }
}
